﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestorDeFlotas.Core;

namespace GestorDeFlotasDesktop.AbmChofer
{
    public partial class AbmChofer : FormABMBase
    {
        public AbmChofer(ModoAbm modoAbm)
            : base(modoAbm)
        {
            InitializeComponent();
        }

        public Chofer MyChofer { get; set; }

        private void AbmChofer_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'gD1C2012DataSet.TL_CHOFERES' table. You can move, or remove it, as needed.
            this.tL_CHOFERESTableAdapter.Fill(this.gD1C2012DataSet.TL_CHOFERES);
            this.HabilitarControles();
        }

        private void HabilitarControles()
        {
            this.btnSeleccionar.Visible = this.modoAbm == ModoAbm.Consulta;
            this.btnAgregar.Visible = this.modoAbm != ModoAbm.Consulta;
            this.grdChoferes.Columns["cho_seleccionar"].Visible = this.modoAbm != ModoAbm.Consulta;
            this.grdChoferes.Columns["cho_eliminar"].Visible = this.modoAbm != ModoAbm.Consulta;
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            LimpiarFiltros();
        }

        private void LimpiarFiltros()
        {
            this.txtApellido.Text = string.Empty;
            this.txtNombre.Text = string.Empty;
            this.txtDni.Text = string.Empty;
            this.RefrescarDataGrid();
        }

        private void RefrescarDataGrid()
        {
            // Codigo para refrescar el DataGridView
            this.gD1C2012DataSet.GetChanges();
            this.tL_CHOFERESTableAdapter.Fill(this.gD1C2012DataSet.TL_CHOFERES);
        }

        private void grdChoferes_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on button cells. 
            if (e.RowIndex < 0 || (e.ColumnIndex !=
                grdChoferes.Columns["cho_seleccionar"].Index && e.ColumnIndex != grdChoferes.Columns["cho_eliminar"].Index)) return;

            // Retrieve the id.
            int choferId = Convert.ToInt32(grdChoferes["cho_id", e.RowIndex].Value);

            if (e.ColumnIndex == grdChoferes.Columns["cho_seleccionar"].Index)
            {
                DatosChofer datosUsuario = new DatosChofer(ModoAbm.Modificacion, new Chofer(choferId).Cargar());
                if (datosUsuario.ShowDialog() == DialogResult.OK)
                {
                    RefrescarDataGridConFiltros();
                }
            }
            else if (e.ColumnIndex == grdChoferes.Columns["cho_eliminar"].Index)
            {
                if ((Boolean)grdChoferes["cho_activo", e.RowIndex].Value)
                {
                    new Chofer(choferId).SetInactivo();
                    RefrescarDataGridConFiltros();
                }
            }
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            RefrescarDataGridConFiltros();
        }

        private void RefrescarDataGridConFiltros()
        {
            int DNI;
            if (this.txtDni.Text == "")
            {
                this.tL_CHOFERESTableAdapter.FillBy_DatosIngresados_SinDNI(this.gD1C2012DataSet.TL_CHOFERES, string.Format("%{0}%", this.txtNombre.Text), string.Format("%{0}%", this.txtApellido.Text));
            }
            else
            {
                DNI = Convert.ToInt32(this.txtDni.Text);
                this.tL_CHOFERESTableAdapter.FillBy_DatosIngresados_ConDNI(this.gD1C2012DataSet.TL_CHOFERES, string.Format("%{0}%", this.txtNombre.Text), string.Format("%{0}%", this.txtApellido.Text), DNI);
            }
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSeleccionar_Click(object sender, EventArgs e)
        {
            if (grdChoferes.SelectedRows.Count < 0) MyChofer = null;

            this.MyChofer = new Chofer((int)grdChoferes["cho_id", grdChoferes.SelectedRows[0].Index].Value);
            this.DialogResult = DialogResult.OK;
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            DatosChofer datosChofer = new DatosChofer(ModoAbm.Alta);
            if (datosChofer.ShowDialog() == DialogResult.OK)
            {
                LimpiarFiltros();
            }
        }
    }
}
